/**
 *author:yhb
 */
package com.towker.frame.service.rbac.role;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;

import com.towker.core.db.*;
import com.towker.core.page.*;
import com.towker.frame.dao.rbac.*;
import com.towker.frame.model.rbac.Role;
import com.towker.frame.model.rbac.Subsystem;
import com.towker.frame.share.ServiceShare;

@Service
public class RoleService extends BaseService<Role, java.lang.Long>
{
	private RoleDao roleDao;
	private RoleUserDao roleUserDao;
	private RoleResourceDao roleResourceDao;
	private SubsystemDao subsystemDao;
	public void setSubsystemDao(SubsystemDao subsystemDao){this.subsystemDao=subsystemDao;}
	public void setRoleDao(RoleDao roleDao){this.roleDao = roleDao;}
	public void setRoleUserDao(RoleUserDao roleUserDao){this.roleUserDao = roleUserDao;}
	public void setRoleResourceDao(RoleResourceDao roleResourceDao){this.roleResourceDao = roleResourceDao;}
	
	@SuppressWarnings("unchecked")
	@Override
	protected EntityDao getEntityDao(){return (EntityDao) this.roleDao;}
	
	/**
	 * 根据id字符串来进行日志删除批处理。
	 */
	public void delByIds(String strIds)
	{
		@SuppressWarnings("unused")
		String[] arrIds = strIds.split(",");
	}
	
	/**
	 * 根据系统编号删除角色以及与角色相关的。
	 */
	public void delRoleAndRelatedById(long id)
	{
		this.roleUserDao.delByRoleid(id);
		this.roleResourceDao.delByRoleid(id);
		this.roleDao.delById(id);
	}
	
	/**根据系统删除所有的角色*/
	public void delRoleAndRelatedBySysid(long sysid){
		this.roleUserDao.delBySysid(sysid);
		this.roleResourceDao.delBySysid(sysid);
	}
	/**根据系统标识删除角色信息*/
	public void delRoleBySystemid(Long systemid){
		this.roleDao.delRoleBySystemid(systemid);
	}
	/**根据用户Id和系统ID获取角色列表*/
	public List<Role> getBySystemAndUser(long sysId,long userId){
		return roleDao.getBySystemAndUser(sysId, userId);
	}
	@SuppressWarnings("unchecked")
	public PageModel getPage(int currentPage, Map map){
		PageRequest request = new PageRequest();
		request.setFilters(map);
		request.setCurrentPage(currentPage);
		request.setPageSize(5);
		PageModel page = roleDao.getPage(request);
		return page;
	}
	/**获得所有的角色*/
	@SuppressWarnings("unchecked")
	public List<Role> getRole(){
		return roleDao.getRole();
	}
	/**根据系统和角色名称过滤条件获得角色数据*/
	public List<Role> getRoleListBySystemAndName(long system, String name){
		return (List<Role>) roleDao.getRoleListBySystemAndName(system, name);
	}
	/**获得系统下的所有角色*/
	@SuppressWarnings("unchecked")
	public List<Role> getRolesBySysid(long sysid){
		return roleDao.getRolesBySysid(sysid);
	}
	/**获得系统角色树*/
	public String getSysRoleTree()
	{
		List<Subsystem> sysList=subsystemDao.getSys();
		List<Role> roleList=roleDao.getRole();
		Map<Subsystem,List<Role>> map=new HashMap<Subsystem, List<Role>>();
		for(Subsystem sys:sysList){
			for(Role role:roleList){
				if(sys.getId().equals(role.getSystemid())){
					boolean flag=false;
					for(Subsystem tmp:map.keySet()){
						if(sys.equals(tmp)){
							map.get(sys).add(role);
							flag=true;
							break;
						}
					}
					if(!flag)
					{
						List<Role> sysRoleList=new ArrayList<Role>();
						sysRoleList.add(role);
						map.put(sys, sysRoleList);
					}
				}
			}
		}
		if(0==map.size())return"[]";
		String str="[";
		for(Subsystem tmp:sysList){
			for(Subsystem sys:map.keySet()){
				if(tmp.equals(sys)){
					str+="{id:"+"'sys_"+sys.getId()+"',pid:0,name:'"+sys.getName()+"'},";
					for(Role role:map.get(sys))str+="{id:"+"'role_"+role.getId()+"',pid:"+"'sys_"+sys.getId()+"',name:'"+role.getName()+"'},";
					break;
				}
			}
		}
		str+="]";
		return new StringBuilder(str).deleteCharAt(str.length()-2)+"";
	}
	/**
	 * 获得用户分配的系统角色树*/
	public String getSysRoleTreeOfUser(long userid)
	{
		List<Subsystem> sysList=subsystemDao.getSys();
		List<Role> roleList=roleDao.getRole();
		List<Role> userRoleList=roleDao.getRoleByUserid(userid);
		for(Role role:roleList)
			for(Role userRole:userRoleList)
				if(role.getId().equals(userRole.getId())){
					role.setAssignToUser(true);break;
				}
		Map<Subsystem,List<Role>> map=new HashMap<Subsystem, List<Role>>();
		for(Subsystem sys:sysList){
			for(Role role:roleList){
				if(sys.getId().equals(role.getSystemid())){
					boolean flag=false;
					for(Subsystem tmp:map.keySet()){
						if(sys.equals(tmp)){
							map.get(sys).add(role);
							flag=true;
							break;
						}
					}
					if(!flag)
					{
						List<Role> sysRoleList=new ArrayList<Role>();
						sysRoleList.add(role);
						map.put(sys, sysRoleList);
					}
				}
			}
		}
		boolean assignedAllRole=true;
		for(Subsystem sys:map.keySet()){
			assignedAllRole=true;
			for(Role role:map.get(sys)){
				if(!role.isAssignToUser()){
					assignedAllRole=false;break;
				}
			}
			sys.setAssignedAllRole(assignedAllRole);
		}
		if(0==map.size())return "[]";
		String str="[";
		for(Subsystem tmp:sysList){
			for(Subsystem sys:map.keySet()){
				if(tmp.equals(sys)){
					str+="{id:"+"'sys_"+sys.getId()+"',pid:0,name:'"+sys.getName()+"',open:true,checked:"+sys.isAssignedAllRole()+"},";
					for(Role role:map.get(sys))str+="{id:"+"'role_"+role.getId()+"',pid:"+"'sys_"+sys.getId()+"',name:'"+role.getName()+"',open:true,checked:"+role.isAssignToUser()+"},";
					break;
				}
			}
		}
		str+="]";
		return new StringBuilder(str).deleteCharAt(str.length()-2)+"";
	}
	
	public static void main(String[] args)
	{
		RoleService service=(RoleService)ServiceShare.getService(RoleService.class);
		System.out.println(service.getSysRoleTree());
	}
	
}
